#!/bin/bash
U=$(id -u)
echo "Execute as user $U"

# check for root
if [ $U -ne 0 ]; then
    echo "Re-exec as root..."
    tmpFile=$(tempfile -m 0755 -p ftdi-tools_ -s .sh)
    SELF=$(readlink -f $0)
    cat $SELF > $tmpFile
    pkexec $tmpFile $*
    rm $tmpFile
    exit 0
fi

if [ "x$1" = "x--install" ] ; then
  echo "Install FTDI drivers"
  UDEV_FILE=/etc/udev/rules.d/60-openocd.rules
  cat > $UDEV_FILE <<EOF
# Copy this file to /etc/udev/rules.d/
# If rules fail to reload automatically, you can refresh udev rules
# with the command "udevadm control --reload"

ACTION!="add|change", GOTO="openocd_rules_end"
SUBSYSTEM!="usb|tty|hidraw", GOTO="openocd_rules_end"

# [GNU MCU Eclipse] -----------------------------------------------------------
# To simplify access, the access rights were changed from:
#   MODE="660", GROUP="plugdev", TAG+="uaccess"
# to:
#   MODE="666"
# -----------------------------------------------------------------------------

# Please keep this list sorted by VID:PID

# opendous and estick
ATTRS{idVendor}=="03eb", ATTRS{idProduct}=="204f", MODE="666"

# Original FT232/FT245 VID:PID
ATTRS{idVendor}=="0403", ATTRS{idProduct}=="6001", MODE="666"

# Original FT2232 VID:PID
ATTRS{idVendor}=="0403", ATTRS{idProduct}=="6010", MODE="666"

# Original FT4232 VID:PID
ATTRS{idVendor}=="0403", ATTRS{idProduct}=="6011", MODE="666"

# Original FT232H VID:PID
ATTRS{idVendor}=="0403", ATTRS{idProduct}=="6014", MODE="666"

# DISTORTEC JTAG-lock-pick Tiny 2
ATTRS{idVendor}=="0403", ATTRS{idProduct}=="8220", MODE="666"

# TUMPA, TUMPA Lite
ATTRS{idVendor}=="0403", ATTRS{idProduct}=="8a98", MODE="666"
ATTRS{idVendor}=="0403", ATTRS{idProduct}=="8a99", MODE="666"

# XDS100v2
ATTRS{idVendor}=="0403", ATTRS{idProduct}=="a6d0", MODE="666"

# Xverve Signalyzer Tool (DT-USB-ST), Signalyzer LITE (DT-USB-SLITE)
ATTRS{idVendor}=="0403", ATTRS{idProduct}=="bca0", MODE="666"
ATTRS{idVendor}=="0403", ATTRS{idProduct}=="bca1", MODE="666"

# TI/Luminary Stellaris Evaluation Board FTDI (several)
ATTRS{idVendor}=="0403", ATTRS{idProduct}=="bcd9", MODE="666"

# TI/Luminary Stellaris In-Circuit Debug Interface FTDI (ICDI) Board
ATTRS{idVendor}=="0403", ATTRS{idProduct}=="bcda", MODE="666"

# egnite Turtelizer 2
ATTRS{idVendor}=="0403", ATTRS{idProduct}=="bdc8", MODE="666"

# Section5 ICEbear
ATTRS{idVendor}=="0403", ATTRS{idProduct}=="c140", MODE="666"
ATTRS{idVendor}=="0403", ATTRS{idProduct}=="c141", MODE="666"

# Amontec JTAGkey and JTAGkey-tiny
ATTRS{idVendor}=="0403", ATTRS{idProduct}=="cff8", MODE="666"

# TI ICDI
ATTRS{idVendor}=="0451", ATTRS{idProduct}=="c32a", MODE="666"

# STMicroelectronics ST-LINK V1
ATTRS{idVendor}=="0483", ATTRS{idProduct}=="3744", MODE="666"

# STMicroelectronics ST-LINK/V2
ATTRS{idVendor}=="0483", ATTRS{idProduct}=="3748", MODE="666"

# STMicroelectronics ST-LINK/V2.1
ATTRS{idVendor}=="0483", ATTRS{idProduct}=="374b", MODE="666"
ATTRS{idVendor}=="0483", ATTRS{idProduct}=="3752", MODE="666"

# STMicroelectronics STLINK-V3
ATTRS{idVendor}=="0483", ATTRS{idProduct}=="374d", MODE="666"
ATTRS{idVendor}=="0483", ATTRS{idProduct}=="374e", MODE="666"
ATTRS{idVendor}=="0483", ATTRS{idProduct}=="374f", MODE="666"
ATTRS{idVendor}=="0483", ATTRS{idProduct}=="3753", MODE="666"

# Cypress KitProg in KitProg mode
ATTRS{idVendor}=="04b4", ATTRS{idProduct}=="f139", MODE="666"

# Cypress KitProg in CMSIS-DAP mode
ATTRS{idVendor}=="04b4", ATTRS{idProduct}=="f138", MODE="666"

# Hilscher NXHX Boards
ATTRS{idVendor}=="0640", ATTRS{idProduct}=="0028", MODE="666"

# Hitex STR9-comStick
ATTRS{idVendor}=="0640", ATTRS{idProduct}=="002c", MODE="666"

# Hitex STM32-PerformanceStick
ATTRS{idVendor}=="0640", ATTRS{idProduct}=="002d", MODE="666"

# Altera USB Blaster
ATTRS{idVendor}=="09fb", ATTRS{idProduct}=="6001", MODE="666"

# Amontec JTAGkey-HiSpeed
ATTRS{idVendor}=="0fbb", ATTRS{idProduct}=="1000", MODE="666"

# SEGGER J-Link
ATTRS{idVendor}=="1366", ATTRS{idProduct}=="0101", MODE="666"
ATTRS{idVendor}=="1366", ATTRS{idProduct}=="0102", MODE="666"
ATTRS{idVendor}=="1366", ATTRS{idProduct}=="0103", MODE="666"
ATTRS{idVendor}=="1366", ATTRS{idProduct}=="0104", MODE="666"
ATTRS{idVendor}=="1366", ATTRS{idProduct}=="0105", MODE="666"
ATTRS{idVendor}=="1366", ATTRS{idProduct}=="0107", MODE="666"
ATTRS{idVendor}=="1366", ATTRS{idProduct}=="0108", MODE="666"
ATTRS{idVendor}=="1366", ATTRS{idProduct}=="1010", MODE="666"
ATTRS{idVendor}=="1366", ATTRS{idProduct}=="1011", MODE="666"
ATTRS{idVendor}=="1366", ATTRS{idProduct}=="1012", MODE="666"
ATTRS{idVendor}=="1366", ATTRS{idProduct}=="1013", MODE="666"
ATTRS{idVendor}=="1366", ATTRS{idProduct}=="1014", MODE="666"
ATTRS{idVendor}=="1366", ATTRS{idProduct}=="1015", MODE="666"
ATTRS{idVendor}=="1366", ATTRS{idProduct}=="1016", MODE="666"
ATTRS{idVendor}=="1366", ATTRS{idProduct}=="1017", MODE="666"
ATTRS{idVendor}=="1366", ATTRS{idProduct}=="1018", MODE="666"

# Raisonance RLink
ATTRS{idVendor}=="138e", ATTRS{idProduct}=="9000", MODE="666"

# Debug Board for Neo1973
ATTRS{idVendor}=="1457", ATTRS{idProduct}=="5118", MODE="666"

# Olimex ARM-USB-OCD
ATTRS{idVendor}=="15ba", ATTRS{idProduct}=="0003", MODE="666"

# Olimex ARM-USB-OCD-TINY
ATTRS{idVendor}=="15ba", ATTRS{idProduct}=="0004", MODE="666"

# Olimex ARM-JTAG-EW
ATTRS{idVendor}=="15ba", ATTRS{idProduct}=="001e", MODE="666"

# Olimex ARM-USB-OCD-TINY-H
ATTRS{idVendor}=="15ba", ATTRS{idProduct}=="002a", MODE="666"

# Olimex ARM-USB-OCD-H
ATTRS{idVendor}=="15ba", ATTRS{idProduct}=="002b", MODE="666"

# USBprog with OpenOCD firmware
ATTRS{idVendor}=="1781", ATTRS{idProduct}=="0c63", MODE="666"

# TI/Luminary Stellaris In-Circuit Debug Interface (ICDI) Board
ATTRS{idVendor}=="1cbe", ATTRS{idProduct}=="00fd", MODE="666"

# TI XDS110 Debug Probe (Launchpads and Standalone)
ATTRS{idVendor}=="0451", ATTRS{idProduct}=="bef3", MODE="666"

# TI Tiva-based ICDI and XDS110 probes in DFU mode
ATTRS{idVendor}=="1cbe", ATTRS{idProduct}=="00ff", MODE="666"

# Ambiq Micro EVK and Debug boards.
ATTRS{idVendor}=="2aec", ATTRS{idProduct}=="6010", MODE="666"
ATTRS{idVendor}=="2aec", ATTRS{idProduct}=="6011", MODE="666"
ATTRS{idVendor}=="2aec", ATTRS{idProduct}=="1106", MODE="666"

# Marvell Sheevaplug
ATTRS{idVendor}=="9e88", ATTRS{idProduct}=="9e8f", MODE="666"

# Keil Software, Inc. ULink
ATTRS{idVendor}=="c251", ATTRS{idProduct}=="2710", MODE="666"

# CMSIS-DAP compatible adapters
ATTRS{product}=="*CMSIS-DAP*", MODE="666"

LABEL="openocd_rules_end"
EOF
  udevadm control -R &&
  echo "Install FTDI driver OK" ||
  echo "Install FTDI driver FAIL"
  exit 0
elif [ "x$1" = "x--uninstall" ] ; then
  SKIP="yes"
  echo "Uninstall FTDI drivers"
  rm -f /etc/udev/rules.d/60-openocd.rules &&
  udevadm control -R &&
  echo "Uninstall FTDI driver OK" ||
  echo "Uninstall FTDI driver FAIL" ||
  exit 0
fi
